/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.systemView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.UserModel;
import com.ruipengkj.service.TUserService;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.memberView.MenberDetailDialog;
import com.ruipengkj.view.memberView.UpdateUserPanel;

/**
 * 说明：
 * 会员卡片查询UI
 * @Author：黄盛强
 * @Date：2012-03-20
 */
public class SelectUserPanel extends BasePagePanel<TUser> implements Runnable{

	private static final long serialVersionUID = 1L;
	//--------------------定义弹出框-----------------------------------
	private UpdateUserPanel updateUserPanelDialog;// 修改用户面板
	
	
	private JLabelJTextFieldComponent cardNumTF = null; //卡号
	private JLabelJTextFieldComponent nameTF = null;//姓名
	private JLabelJTextFieldComponent tel = null;//电话
	private DataPackageComponent startTimeTF = null;//发卡时间
	private DataPackageComponent timeTF = null;//时间
	
	
	private JButton queryButton = null;//查询按钮
	private JButton cleateButton = null;//清空按钮
	//--------------------------------------------------
	private JButton delButton =null;   // 删除按钮
	private JButton updateButton =null;   // 删除按钮

	private JTable table;
	//private MemberQueryCardModel memberQueryCardModel;
	private UserModel userModel;
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TUser> pageView;//翻页主键
	private JScrollPane tableJsp;
	private TUserService tUserService;	
	private MenberDetailDialog menberDetailDialog;//会员详细窗体
	public SelectUserPanel() {
		tUserService=BuildSpringApplication.getInstance().getTUserServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
//		ApplicationContext.CardUtilThread.setFlag(false);//停止之前线程
//		ApplicationContext.CardUtilThread=new CardUtil(){//重新定义线程
//			protected void send(String drive){
//				cardNumTF.getText().setText(drive);
//				cardNumTF.requestFocus();
//			}
//		};
//		ApplicationContext.CardUtilThread.startOn(cardNumTF.getText());//启动线程
	}
	
	private void init() {
		pageView=new PageView<TUser>();
		userModel=new UserModel();
		table=uiFactory.createJTable(userModel, null, this, 30, JLabel.CENTER);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tableJsp=new JScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		this.add(this.topPanel(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(pagingComponent, BorderLayout.SOUTH);
		//排序
		TableRowSorter<UserModel> tableRowSorter = new TableRowSorter<UserModel>(userModel);  
		table.setRowSorter(tableRowSorter);
	}
	
	private JPanel topPanel(){
		JPanel jp=new JPanel(new FlowLayout(FlowLayout.LEFT));
		tel = new JLabelJTextFieldComponent("手机：", 15);
		cardNumTF = new JLabelJTextFieldComponent("卡号：", 15);
		nameTF = new JLabelJTextFieldComponent("姓名：", 15);
		startTimeTF = new DataPackageComponent("发卡时间：", 15);
		timeTF = new DataPackageComponent("时间：", 15);
		queryButton = new JButton("查询");
		cleateButton= new JButton("清空");
		delButton =new JButton("删除");
		updateButton =new JButton("修改");
		
		jp.add(cardNumTF);
		jp.add(nameTF);
		jp.add(tel);
		jp.add(startTimeTF);
		jp.add(timeTF);
		jp.add(queryButton);
		jp.add(cleateButton);
		jp.add(updateButton);
		
		//-----------------------------
		jp.add(delButton); 
		return jp;
	}

	private void initListener(){
		queryButton.addActionListener(this);
		cleateButton.addActionListener(this);
		nameTF.getText().addKeyListener(this);
		tel.getText().addKeyListener(this);
		//-------------------------------------------
		delButton.addActionListener(this);
		delButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		updateButton.addActionListener(this);
		updateButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	//条件查询
	protected void termQuery(final int currentPage) {
		final TUser tuser=new TUser();
	
		tuser.setCardNum(cardNumTF.getText().getText().trim());
		tuser.setRealName(nameTF.getText().getText().trim());
		tuser.setSendCardTime(startTimeTF.getDate());
		tuser.setMobile(tel.getText().getText());
		tuser.setEndTime(timeTF.getDate());
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TUser> pageModel=tUserService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tuser);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						Util.clearTableRowSorter(table);
						userModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								Util.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						waitComponent.setText("不存在数据");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TUser> getPageView() {
		return pageView;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询
			this.termQuery(1);
		}
		
		else if(e.getSource()==cleateButton){
			//清除
			cardNumTF.getText().setText("");
			nameTF.getText().setText("");
			startTimeTF.setDate(null);
			timeTF.setDate(null);
		}else if(e.getSource()==delButton){ //---------------------------------------------------
			// 删除商品按钮
			this.deleteUser();
			
		}
		else if (e.getSource() == updateButton) {
			// 修改按钮
			this.modifyUser();
		} 
	}
	
	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_ENTER){
			if(e.getSource()==nameTF.getText()||e.getSource() == tel.getText()){
				//查询会员
				queryButton.doClick();
			} else if (e.getSource() == table) { //--------------------------------------------------
				if (e.getKeyCode() == KeyEvent.VK_DELETE) {
					// 删除商品
					this.deleteUser();
				}
			}
			else if (e.getSource() == updateButton) {
				// 修改按钮
				this.modifyUser();
			} 
		}
		
	}
	@Override
	public void mouseClicked(MouseEvent e) {
    	if(e.getSource()==table&&e.getClickCount() == 2){
    		//表格
    		String id=table.getValueAt(table.getSelectedRow(), 0).toString();
    		try {
				TUser user=tUserService.getByIdService(id);
				if(user==null){
					JOptionPane.showMessageDialog(null, "当前会员不存在");
					return;
				}
				
				//menberDetailDialog=new MenberDetailDialog(null, "会员详细", true);
	    		//menberDetailDialog.showDialog(user);
				
				updateUserPanelDialog = new UpdateUserPanel(null, "会员详细", true);
				updateUserPanelDialog.showDialog(user);
				if(updateUserPanelDialog.isSuccess==true){
					this.termQuery(1);
				}
				
			} catch (Exception e1) {
				e1.printStackTrace();
			}
    	}
    }
	//------------------------------------------------------------------------------------
	
	// 删除用户
	private void deleteUser() {
		if (table.getRowCount() != 0) {
			int row = table.getSelectedRow();
			if (row != -1) {
				if (Util.showConfirmDialog("是否要删除选择记录") == 0) {
					String userID = (String) table.getValueAt(row, 0);
					try {
						tUserService.deleteService(userID);
						JOptionPane.showMessageDialog(null, "删除成功");
						this.termQuery(1);
					} catch (Exception e1) {
						JOptionPane.showMessageDialog(null, "删除失败");
						e1.printStackTrace();
					}
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择行在进行删除");
				return;
			}
		}
	}
	//----------------------------修改用户
	
	// 修改商品
	private void modifyUser() {
		if (table.getRowCount() != 0) {
			int row = table.getSelectedRow();
			if (row != -1) {
				String userid = (String) table.getValueAt(row, 0);
				try {
					TUser tuser = tUserService
							.getByIdService(userid);
					updateUserPanelDialog = new UpdateUserPanel(null, "会员详细", true);
					updateUserPanelDialog.showDialog(tuser);
					if(updateUserPanelDialog.isSuccess==true){
						this.termQuery(1);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择行在进行修改");
				return;
			}
		}
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	
	
}
